<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Hospital extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'hospital';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'service_list',
        'department_list',
        'level_text',
        'status_text',
        'image_str',

    ];



    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }


    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getLevelList()
    {
        return ['0' => __('Level 0'), '1' => __('Level 1'), '2' => __('Level 2'), '3' => __('Level 3'), '4' => __('Level 4'), '5' => __('Level 5'), '6' => __('Level 6'), '7' => __('Level 7'), '8' => __('Level 8')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getLevelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['level']) ? $data['level'] : '');
        $list = $this->getLevelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDepartmentListAttr($value, $data)
    {
        return Department::whereIn('id', $data['dep_ids'])->select();
    }


    public function department()
    {
        return $this->belongsTo('Department', 'dep_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getServiceListAttr($value, $data)
    {
        return Service::whereIn('id', $data['category_ids'])->select();
    }

    public function category()
    {
        return $this->hasMany('app\common\model\service\Category', 'id', 'category_ids');
    }


    public function city()
    {
        return $this->belongsTo('City', 'city_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
